package com.example.tetris.Tetris;

import com.example.tetris.Tetris.TetrisListener;
import com.example.tetris.beans.MyToastdialog;
import ohos.aafwk.ability.Ability;

import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.*;

public class TetrisBase extends TetrisListener {
    //定义有戏1界面中所有小方格的数据结构，一个二维列表
    //0：定义小方格 1：已经下落的方块
    protected List<List<Integer>> tetrisData = new ArrayList<>();
    protected Ability ability;
    protected Component component;


    //小方格之间的距离
    protected int cellSpace = 10;
    //小方格的尺寸
    protected int cellSize = 50;
    //水平方向小方格的数量
    protected int colCount = 0;
    //垂直方向小方格数量
    protected int rowCount = 0;
    //用于绘制方块的数据结构
    protected int[][] drawBlock;
    //用于保存所有方块的定义
    protected List<int[][]> blocks = new ArrayList<>();
    //用于保存困难模式所有方块的定义
    protected List<int[][]> blocks_df = new ArrayList<>();

    //方块可以放到tetris的当前位置,final修饰变量：不可被重新赋值
    protected final int TETRIS_BLOCK_CONTINUE = 0;
    //方块遇到已落下的方块，停止移动
    protected final int TETRIS_BLOCK_STOPPED = 1;
    //方块超出左边界
    protected final int TETRIS_BLOCK_LEFT_OVERFLOW = 2;
    //方块超出下边界
    protected final int TETRIS_BLOCK_DOWN_OVERFLOW = 3;
    //方块超出右边界
    protected final int TETRIS_BLOCK_RIGHT_OVERFLOW = 4;
    //方块超出上边界
    protected final int TETRIS_BLOCK_UP_OVERFLOW = 5;

//    protected Color blockColor = Color.RED;


    protected int[][] currentBlock;
    public int[][] nextBlock;
    protected int blockCurrentRow = 0, blockCurrentCol = 0;
    protected int blockCurrentOrientation = 0;
    //定义一个定时器，用于方块自动下落
    public Timer timer;

    //判断是否开始游戏
    public boolean isPlaying = false;
    //判断游戏是否结束，防止加速键在游戏结束后还可用，导致重复插入数据
    protected boolean isOver = false;
    //判断游戏模式
    public String mode_flag;




    //构造方法的第一种重载形式--------------------------------------------
    public TetrisBase(Ability ability, Component component,int[][] drawBlock) {


        this.drawBlock = drawBlock;
        this.ability = ability;
        //component指定了与Tetris组件结合的组件，游戏界面就绘制在这个组件中
        this.component = component;
        //设置组件的绘制任务，这样当component刷新时，onDraw方法就会被调用
        component.addDrawTask(this);
        //让组件允许单击，这样可以单击组件让方块顺时针旋转
        component.setClickable(true);
        //获取组件宽度
        int width = component.getWidth();
        //获取组件高度
        int height = component.getHeight();


        //如果未指定drawBlock，就是绘制左侧游戏界面
        if (drawBlock == null) {
            colCount = (width - cellSpace) / (cellSize + cellSpace);
            rowCount = (height - cellSpace) / (cellSize + cellSpace);
        } else {
            //如果指定drawBlock,则绘制方块
            rowCount = drawBlock.length;
            colCount = drawBlock[0].length;
            currentBlock = drawBlock;
            blockCurrentRow = 0;
            blockCurrentCol = 0;
        }
        //初始化俄罗斯方块数据，将所有列表元素逗设置为0，表示初始状态，背景中没有任何方块
        for (int i = 0; i < rowCount; i++) {
            List<Integer> rowList = new ArrayList<>();
            for (int j = 0; j < colCount; j++) {
                rowList.add(0);
            }
            tetrisData.add(rowList);
        }


        //初始化方块，1表示有小方格，0表示无小方格
        int[][] block1 = new int[][]{{1, 1, 0}, {0, 1, 1}};
        blocks.add(block1);
        int[][] block2 = new int[][]{{0, 1, 0}, {1, 1, 1}};
        blocks.add(block2);
        int[][] block3 = new int[][]{{1, 1}, {1, 1}};
        blocks.add(block3);
        int[][] block4 = new int[][]{{0, 0, 1}, {1, 1, 1}};
        blocks.add(block4);
        int[][] block5 = new int[][]{{1, 1, 1, 1}};
        blocks.add(block5);
        //困难模式方块
        int[][] block_df1=new int[][]{{0,1,1},{1,1,0},{0,1,0}};
        blocks_df.add(block_df1);
        int[][] block_df2=new int[][]{{1,1,1,1,1}};
        blocks_df.add(block_df2);
        int[][] block_df3=new int[][]{{1,0},{1,0},{1,0},{1,1}};
        blocks_df.add(block_df3);
        int[][] block_df4=new int[][]{{1,1},{1,1},{1,0}};
        blocks_df.add(block_df4);
        int[][] block_df5=new int[][]{{1,1,1},{0,1,0},{0,1,0}};
        blocks_df.add(block_df5);
        int[][] block_df6=new int[][]{{1,0,1},{1,1,1}};
        blocks_df.add(block_df6);
        int[][] block_df7=new int[][]{{1,1,0},{0,1,0},{0,1,1}};
        blocks_df.add(block_df7);
    }

    //构造方法第二种重载形式,绘制游戏界面--------------------------------------------
    public TetrisBase(Ability ability, Component component) {
        this(ability, component,null);
    }

    //   判断当前位置是否可以绘制方块,根据情况返回上面定义的6个常量中一个--------------------------------------------
    public int verifyBlockLocation(int[][] block, int startRow, int startCol) {
        //获取方块行数
        int blockRowCount = block.length;
        //获取方块列数
        int blockColCount = block[0].length;
        //在双重for循环中同时处理了第一种和第二种情况
        //扫描方块的每一个小方格，如过小方格值为1，而且小方格在tetrisData中的位置的值也为1，则
        //返回TETRIS_BLOCK_STOPPED ，如果小方格值和在tetrisData中的位置有一个为0，则继续
        //扫描下一个小方格
        for (int i = 0; i < blockRowCount; i++) {
            for (int j = 0; j < blockColCount; j++) {
                //计算方块在Tetris中的行
                int rowInTetris = i + startRow;
                //计算方块在Tetris中的列
                int colInTetris = j + startCol;
                //超出下边界
                if (rowInTetris >= rowCount) {
                    return TETRIS_BLOCK_DOWN_OVERFLOW;
                }
                //超出上边界
                if (rowInTetris < 0) {
                    return TETRIS_BLOCK_UP_OVERFLOW;
                }
                // 超出右边界
                if (colInTetris >= colCount) {
                    return TETRIS_BLOCK_RIGHT_OVERFLOW;
                }
                //超出左边界
                if (colInTetris < 0) {
                    return TETRIS_BLOCK_LEFT_OVERFLOW;
                }
                //此处已有下落的方块，只用判断下方，左右不用判断
                if (block[i][j] == 1) {
                    if (tetrisData.get(rowInTetris).get(colInTetris) == 1) {
                        return TETRIS_BLOCK_STOPPED;
                    }
                }
            }
        }
        return TETRIS_BLOCK_CONTINUE;

    }

    //绘制小方格，背景小方格或者是方块，区别是颜色不同而已
//row:小方格的行  col:小方格的列  color:小方格颜色--------------------------------------------
    protected void drawCell(Canvas canvas, int row, int col, Color color) {
        //row和col必须比游戏界面的行和列小才可绘制
        if (row > rowCount || col > colCount) {
            return;
        }
        //计算中间的实心小方格与外框的距离
        float margin = cellSize / 6;
        Paint paint = new Paint();
        //设置线宽
        paint.setStrokeWidth(4);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setColor(color);
        //计算小方格外框的左上角横坐标
        float left = col * cellSize + cellSpace * (col + 1);
        //计算小方格外框的左上角纵坐标
        float top = row * cellSize + cellSpace * (row + 1);
        float right = left + cellSize;
        float bottom = top + cellSize;
        //绘制小方格外框(空心的)
        canvas.drawRect(new RectFloat(left, top, right, bottom), paint);
        //绘制实心小方格
        paint.setStyle(Paint.Style.FILL_STYLE);
        //在空心小方格的基础上加上设置好的内边距
        canvas.drawRect(new RectFloat(left + margin, top + margin, right - margin, bottom - margin), paint);

    }

    //旋转方块,orientation为旋转角度--------------------------------------------
    public int[][] rotateBlock(int[][] block, int orientation) {
        //行数，纵向方块数量
        int row = block.length;
        //列数，横向方块数量
        int col = block[0].length;
        int[][] result = null;
        switch (orientation) {
            case 0://旋转0°
                result = block;
                break;
            case 1://旋转90°
                result = new int[col][row];
                for (int i = 0; i < row; i++) {
                    for (int j = 0; j < col; j++) {
                        result[j][row - i - 1] = block[i][j];
                    }
                }
                break;
            case 2://旋转180°
                result = new int[row][col];
                for (int i = 0; i < row; i++) {
                    for (int j = 0; j < col; j++) {
                        result[i][j] = block[row - i - 1][col - j - 1];
                    }
                }
                break;
            case 3://旋转270°
                result = new int[col][row];
                for (int i = 0; i < row; i++) {
                    for (int j = 0; j < col; j++) {
                        result[col - j - 1][i] = block[i][j];
                    }
                }
                break;
        }
        //重新刷新component，也就是再次调用onDraw方法绘制旋转后的方块
        component.invalidate();
        return result;

    }

    //顺时针旋转方块，blockIndex表示blocks中方块的索引，orientation表示旋转方向，0-3--------------------------------------------
    public int[][] rotateBlock(int blockIndex, int orientation) {
        //随机获取一种方块
        int[][] block=null;
        if(Objects.equals(mode_flag, "difficulty")){
            block = blocks_df.get(blockIndex);
        }else{
            block = blocks.get(blockIndex);
        }

        //顺时针旋转方块
        return rotateBlock(block, orientation);
    }

    //随机产生方块--------------------------------------------
    protected int[][] generateNextBlock() {
        Random random = new Random();
        //随机产生方块的索引(0-4),带参的nextInt(int x)则会生成一个范围在0~x（不包含X）内的任意正整数
        int blockIndex =0;
        if(Objects.equals(mode_flag, "difficulty")){
            blockIndex = random.nextInt(7);
        }else{
            blockIndex = random.nextInt(5);
        }

        //随机方块的旋转角度，从0-3，表示顺时针旋转0°、90°、180°和270°
        int blockOrientation = random.nextInt(4);
        //旋转后的方块
        return rotateBlock(blockIndex,blockOrientation);
    }

    // 随机产生的block的列，行不需要随机产生，永远是0--------------------------------------------
    protected int getBlockStartCol(int[][] block) {
        if (block.length > 0) {
            int blockCol = block[0].length;
            return new Random().nextInt(colCount - blockCol - 2);
        } else {
            return 0;
        }
    }


    //消除指定的行--------------------------------------------
    public void removeRowFromTetris(int row) {
        //判断是否超出组件范围
        if (row < 0 || row > rowCount) {
            return;
        }
        //用上一行小方格覆盖当前行小方格
        if (row > 0) {
            for (int i = row; i > 0; i--) {
                for (int j = 0; j < colCount; j++) {
                    tetrisData.get(i).set(j, tetrisData.get(i - 1).get(j));
                }
            }
        }
        //如果消除或移动第一行，那么就将第一行小方格全部设置为0
        for (int j = 0; j < colCount; j++) {
            tetrisData.get(0).set(j, 0);
        }
    }

    //消除行--------------------------------------------
    public int removeRowsFromTetris() {
        int removeRowCount = 0;//被消除的行数
        //从第1行（索引为0）开始扫描
        for (int i = 0; i < rowCount; i++) {
            //记录当前行有多少个小方格值为1，如果个数与colCount相同，则消除此行
            int count = 0;
            for (int j = 0; j < colCount; j++) {
                //一行中只要有一个小方格值为0则不能消除
                if (tetrisData.get(i).get(j) == 0) {
                    continue;
                }
                count += 1;
            }
            //消除此行
            if (count == colCount) {
                //消除特定的行
                removeRowFromTetris(i);
                //消除的行数加1
                removeRowCount += 1;
            }
        }
        if (removeRowCount > 0) {
            if (tetrisRowRemovedListener != null) {
                //如果消除了至少一行，并且设置了消除行事件监听器，则触发消除事件
                tetrisRowRemovedListener.rowRmoved(removeRowCount);
            }
        }
        return removeRowCount;
    }

    //方块归位，按照方块落下的最后位置重新设置tetrisData的值
    //startRow和startCol是方块在tetrisData中的行和列--------------------------------------------
    public void setBlock(int[][] block, int startRow, int startCol) {
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                if (i >= startRow && i < startRow + block.length) {
                    if (j >= startCol && j < startCol + block[0].length) {
                        if (block[i - startRow][j - startCol] == 1) {
                            tetrisData.get(i).set(j, 1);
                        }
                    }
                }
            }
        }
    }

    //从startRow和StartCol位置开始绘制方块--------------------------------------------
    public void drawBlock(Canvas canvas, int[][] block, int startRow, int startCol) {
//        如果游戏没开始不能绘制
        if (!isPlaying && drawBlock == null) {
            return;
        }
        //如果drawBlock为null，表明绘制的是游戏背景小方格
        if (drawBlock == null) {
            //开始绘制是要判断，是否可以绘制在当前位置
            //如果可以，继续绘制，并且设置方块的属性()
            int blockLocation = verifyBlockLocation(block, startRow, startCol);
            if (blockLocation == TETRIS_BLOCK_CONTINUE) {
                //如果可以绘制方块，检测方块的下一个位置（row+1）是否可以移动，如果不可以，说明该位置是方块最后一个位置
                int nextBlockLocation = verifyBlockLocation(block, startRow + 1, startCol);
                //不能再向下移动了，碰到底部或者碰到已经下落的方块
                if (nextBlockLocation == TETRIS_BLOCK_DOWN_OVERFLOW || nextBlockLocation == TETRIS_BLOCK_STOPPED) {
                    //下面这一部分和onStart方法一致
                    blockCurrentRow = 0;
                    //将产生的下一个方块作为当前的方块
                    currentBlock = nextBlock;
                    //产生下一个方块
                    nextBlock = generateNextBlock();
                    //获取新产生方块的列位置
                    blockCurrentCol = getBlockStartCol(block);

                    if (tetrisNextBlockListener != null) {
                        //如果设置了事件监听器，则触发下一个方块事件
                        tetrisNextBlockListener.onNextBlock(nextBlock);
                    }

                    //如果方块不能继续向下走了，就应将正在下落的方块归位，变成游戏背景小方格一部分
                    setBlock(block, startRow, startCol);
                    //消除行
                    removeRowsFromTetris();
                    //让UI再次刷新（重新调用onDraw方法），并绘制最新的游戏元素
                    component.invalidate();
                    return;

                }
            } else {
                //如果不能绘制方块，要先停止游戏
                stop();
                isOver=true;
                MyToastdialog.showTip(ability,"判断类型"+blockLocation);

                if (tetrisGameoverListener != null) {
                    //触发游戏结束事件
                    tetrisGameoverListener.onGameOver();

                }
                return;
            }
        }


        //绘制方块，如果指定了drawBlock，那么就只绘制方块
        //其实这时block和drawBlock值相同，通过block参数也会传递drawBlock的值
        //只是为了做的更通用，才通过参数传入block
        //如果block为空，此处不会执行
        for (int i = 0; i < block.length; i++) {
            for (int j = 0; j < block[i].length; j++) {
                if (block[i][j] == 1) {
                    drawCell(canvas, startRow + i, startCol + j, judgeBlockColor(block));
                }
            }
        }


    }

    //随机残局
    public void endGame(){
        Random random = new Random();
        if(Objects.equals(mode_flag, "endgm")){
            for (int i = rowCount-1; i > rowCount*0.4; i--) {
                for (int j = 0; j < colCount; j++) {
                    //产生0-2的随机数，不包括2
                    int number = random.nextInt(2);
                    tetrisData.get(i).set(j, number);
                }
            }
        }
    }


    //不同方块上不同颜色
    public Color judgeBlockColor(int[][] block) {
        Color block1 = Color.MAGENTA;
        Color block2 = Color.BLUE;
        Color block3 = Color.GREEN;
        Color block4 = Color.YELLOW;
        Color block5 = Color.CYAN;
        Color block6 = Color.DKGRAY;
        Color block7 = Color.GRAY;
        Color fault = Color.RED;

        if(Objects.equals(mode_flag, "difficulty")){
            //3行3列
            if(block.length==3){
                if (block[0].length == 3) {
                    if(block[0][0] == 0 && block[1][2] == 0 && block[2][0] == 0 && block[2][2] == 0) return block1;
                    if(block[0][0] == 0 && block[0][2] == 0 && block[2][0] == 0 && block[2][1] == 0) return block1;
                    if(block[0][0] == 0 && block[0][2] == 0 && block[1][0] == 0 && block[2][2] == 0) return block1;
                    if(block[0][1] == 0 && block[0][2] == 0 && block[2][0] == 0 && block[2][2] == 0) return block1;
                    if(block[1][0] == 0 && block[1][2] == 0 && block[2][0] == 0 && block[2][2] == 0) return block2;
                    if(block[0][0] == 0 && block[0][1] == 0 && block[2][0] == 0 && block[2][1] == 0) return block2;
                    if(block[0][0] == 0 && block[0][2] == 0 && block[1][0] == 0 && block[1][2] == 0) return block2;
                    if(block[0][1] == 0 && block[0][2] == 0 && block[2][1] == 0 && block[2][2] == 0) return block2;
                    if(block[0][2] == 0 && block[1][0] == 0 && block[1][2] == 0 && block[2][0] == 0) return block7;
                    if(block[0][0] == 0 && block[0][1] == 0 && block[2][1] == 0 && block[2][2] == 0) return block7;
                }
            }
            //5列1行
            if (block.length == 1) {
                if (block[0][0] == 1 && block[0][1] == 1 && block[0][2] == 1 && block[0][3] == 1&& block[0][4] == 1) return block2;
            }
            //1行5列
            if (block.length == 5) {
                if (block[0][0] == 1 && block[1][0] == 1 && block[2][0] == 1 && block[3][0] == 1&& block[4][0] == 1) return block2;
            }
            //block_df3 4行2列
            if (block.length == 4) {
                if (block[0].length == 2) {
                    if(block[0][0] == 1 && block[1][0] == 1 && block[2][0] == 1 && block[3][0] == 1&& block[3][1] == 1) return block3;
                    if(block[0][0] == 1 && block[0][1] == 1 && block[1][1] == 1 && block[2][1] == 1&& block[3][1] == 1) return block3;
                }
            }
            //block_df3 2行4列
            if (block.length == 2) {
                if (block[0].length == 4) {
                    if(block[0][0] == 1 && block[0][1] == 1 && block[0][2] == 1 && block[0][3] == 1&& block[1][0] == 1) return block3;
                    if(block[0][3] == 1 && block[1][0] == 1 && block[1][1] == 1 && block[1][2] == 1&& block[1][3] == 1) return block3;
                }
            }
            //block_df4、6  2行3列
            if (block.length == 2) {
                if (block[0].length == 3) {
                    if(block[1][0] == 0) return block4;
                    if(block[0][2] == 0) return block4;
                    if(block[0][1] == 0) return block6;
                    if(block[1][1] == 0) return block6;


                }
            }
            //block_df4、6  3行2列
            if (block.length == 3) {
                if (block[0].length == 2) {
                    if(block[2][1] == 0) return block4;
                    if(block[0][0] == 0) return block4;
                    if(block[1][1] == 0) return block6;
                    if(block[1][0] == 0) return block6;

                }
            }


        }else{
            //3行2列
            if (block.length == 3) {
                if (block[0].length == 2) {
                    if (block[0][1] == 1 && block[1][0] == 1 && block[1][1] == 1 && block[2][0] == 1) return block1;

                    if (block[0][1] == 1 && block[1][0] == 1 && block[1][1] == 1 && block[2][1] == 1) return block2;

                    if (block[0][0] == 1 && block[1][0] == 1 && block[1][1] == 1 && block[2][0] == 1) return block2;

                    if (block[0][0] == 1 && block[0][1] == 1 && block[1][1] == 1 && block[2][1] == 1) return block3;

                    if (block[0][0] == 1 && block[1][0] == 1 && block[2][0] == 1 && block[2][1] == 1) return block3;

                }
            }
            //2行3列
            if (block.length == 2) {
                if (block[0].length == 3) {

                    //block1
                    if (block[0][0] == 1 && block[0][1] == 1 && block[1][1] == 1 && block[1][2] == 1) return block1;

                    //block2
                    if (block[0][1] == 1 && block[1][0] == 1 && block[1][1] == 1 && block[1][2] == 1) return block2;


                    if (block[0][0] == 1 && block[0][1] == 1 && block[0][2] == 1 && block[1][1] == 1) return block2;


                    //block4
                    if (block[0][2] == 1 && block[1][0] == 1 && block[1][1] == 1 && block[1][2] == 1) return block3;


                    if (block[0][0] == 1 && block[0][1] == 1 && block[0][2] == 1 && block[1][0] == 1) return block3;

                }
            }
            //2列2行
            if (block.length == 2) {
                if (block[0].length == 2) {
                    //block3
                    if (block[0][0] == 1 && block[0][1] == 1 && block[1][0] == 1 && block[1][1] == 1) return block4;

                }
            }
            //4列1行
            if (block.length == 1) {
                //block5
                if (block[0][0] == 1 && block[0][1] == 1 && block[0][2] == 1 && block[0][3] == 1) return block5;

            }
            //1行4列
            if (block.length == 4) {
                if (block[0][0] == 1 && block[1][0] == 1 && block[2][0] == 1 && block[3][0] == 1) return block5;

            }
        }





        return fault;
    }


    //定时器相关配置，定时器使用指定的事件频率执行TetrisTask.run，

    //在run方法中会不断让方块的当前行（blockCurrentRow）加1，
//这样就可以使方块不断下移--------------------------------------------
    public class TetrisEventHandler extends EventHandler {
        public TetrisEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override//帮助自己检查是否正确的复写了父类中已有的方法;告诉读代码的人，这是一个复写的方法
        public void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            //方块的当前块加1，让方块不断向下移动
            blockCurrentRow += 1;
            //刷新UI
            component.invalidate();
        }
    }

    public class TetrisTask extends TimerTask {
        @Override
        public void run() {
            EventRunner runner = EventRunner.getMainEventRunner();
            TetrisEventHandler handler = new TetrisEventHandler(runner);
            handler.sendEvent(0);
            runner = null;
        }
    }

    //开始游戏--------------------------------------------
    public void start() {
        //每次都重新初始化游戏
        timer = new Timer();
        System.out.println("-------------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>游戏开始");
        if(Objects.equals(mode_flag, "endgm")){
            endGame();
        }
        // 产生一个正常处理的Block，因为这个Block还不知道能否放到Tetris中，根据处理结果，决定是否更新currentBlock
        //重新初始化与方块相关的变量
        blockCurrentRow = 0;
        //随机产生当前方块，也就是第一个出现的方块
        currentBlock = generateNextBlock();
//        HiLog.info(label, Arrays.toString(currentBlock));
        //产生下一个显示的方块，也就是第二个出现的方块
        nextBlock = generateNextBlock();

        //触发下一个方块事件
        if (tetrisNextBlockListener != null) {
            tetrisNextBlockListener.onNextBlock(nextBlock);
        }

        //获取当前方块的列
        blockCurrentCol = getBlockStartCol(currentBlock);
        //启动定时器,从firstTime(0)时刻开始，每隔period(800)毫秒执行一次。
        timer.schedule(new TetrisTask(), 0, 800);
        isPlaying = true;
    }

    //停止游戏--------------------------------------------
    public void stop() {

        //取消定时器
        timer.cancel();
        isPlaying = false;
    }

    //快速下落与正常下落之间切换--------------------------------------------
    //快速下落
    public void moveQuickly() {
        if(!isOver){
            stop();
            timer = new Timer();
            //使用执行时间间隔为100毫秒的定时器
            timer.schedule(new TetrisTask(), 0, 100);
            isPlaying = true;
        }

    }

    //恢复正常下落速度
    public void moveNormally() {
        if(!isOver){
            stop();
            timer = new Timer();
            //使用执行时间间隔为800毫秒的定时器
            timer.schedule(new TetrisTask(), 0, 800);
            isPlaying = true;

        }
    }

    //左右移动，通过修改列值实现--------------------------------------------
    //向左水平移动
    public void moveLeft() {
        if (isPlaying) {
            int result = verifyBlockLocation(currentBlock, blockCurrentRow, blockCurrentCol - 1);
            //判断方块是否超过左边界
            if (result != TETRIS_BLOCK_LEFT_OVERFLOW && result != TETRIS_BLOCK_STOPPED) {
                //未超出左边界，当前列值减一，方块水平向左移动
                blockCurrentCol -= 1;
                component.invalidate();
            }


        }
    }

    //向右水平移动
    public void moveRight() {
        if (isPlaying) {
            int result = verifyBlockLocation(currentBlock, blockCurrentRow, blockCurrentCol + 1);
            //判断方块是否超过右边界
            if (result != TETRIS_BLOCK_RIGHT_OVERFLOW && result != TETRIS_BLOCK_STOPPED) {
                //未超出右边界，当前的列值加一，方块水平向右移动
                blockCurrentCol += 1;
                component.invalidate();
            }

        }
    }

}
